package com.hualan.sfzy.service.mindanswer.serviceimpl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.sfzy.constant.CommonConstant;
import com.hualan.sfzy.entity.mindanswer.Problem;
import com.hualan.sfzy.mapper.mindanswer.ProblemMapper;
import com.hualan.sfzy.service.mindanswer.ProblemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("problemService")
public class ProblemServiceImpl implements ProblemService {
    @Autowired
    private ProblemMapper mapper;

    public  int add(Problem problem){
        Date date=new Date();
        problem.setCreateTime(date);
        return mapper.insert(problem);
    }
    public int remove(Integer id){
        return mapper.delete(id);
    }
    public int removeBatch(Integer[] ids){
        return mapper.deleteBatch(ids);
    }
    public int edit(Problem problem){
        return mapper.update(problem);
    }
    public Problem findById(Integer id){
        return mapper.selectById(id);
    }
    public List<Problem> find(){
        return mapper.select(null);
    }

    public Map<String,Object> findByPage(Integer pageNum, Integer pageSize, Problem problem){
        System.out.println(pageNum+" "+pageSize);

        PageHelper.startPage(pageNum, pageSize);
        List<Problem> list = mapper.select(problem);
        PageInfo<Problem> pageInfo = new PageInfo<>(list);
        Map<String , Object> map = new HashMap<>();
        map.put(CommonConstant.PAGE_LIST, list);
        map.put(CommonConstant.PAGE_TOTAL, pageInfo.getPages());
        map.put(CommonConstant.TOTAL, pageInfo.getTotal());
        return map;
    }
   public Map<String, Object> findByPageUser(Integer pageNum, Integer pageSize, Problem entity){
        PageHelper.startPage(pageNum, pageSize);
        List<Problem> list = mapper.selectByUser(entity);
        PageInfo<Problem> pageInfo = new PageInfo<>(list);
        Map<String , Object> map = new HashMap<>();
        map.put(CommonConstant.PAGE_LIST, list);
        map.put(CommonConstant.PAGE_TOTAL, pageInfo.getPages());
        map.put(CommonConstant.TOTAL, pageInfo.getTotal());
        return map;
    }
}
